<template>
  <div class="app-container">
    <el-container>

      <!--左边疾病目录表-->
      <el-aside width="350px" style="border: 1px solid #eee; background-color: white">
        <el-form :model="queryParamsType" ref="queryForm" :inline="true" v-show="showSearchType" label-width="68px"  style="height: 50px;padding-left: 20px">
          <el-col :span="16">
            <el-input
              v-model="queryParamsType.name"
              placeholder="请输入类别名"
              clearable
              size="small"
              @keyup.enter.native="handleQueryType"
            />
          </el-col>
          <el-col :span="1.0">
            <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQueryType"></el-button>
          </el-col>
        </el-form>
        <el-col :span="1.5" style="height: 30px">
          <span style="padding:0px 90px 0px 15px; font-size: 14px;">诊断分类目录</span>
          <el-button
            type="text"
            icon="el-icon-plus"
            size="mini"
            @click="handleAddType"
            v-hasPermi="['dms:dise_catalog:add']"
          >新增</el-button>
        </el-col>
          <el-table v-loading="loadingType" :data="dise_catalogList" :show-header="false" @row-click="handleQuery" style="height: 600px;width:400px;overflow: auto;">
          <el-table-column label="诊断分类目录" align="center" prop="name"/>
          <el-table-column align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdateType(scope.row)"
                v-hasPermi="['dms:dise_catalog:edit']"
              >修改</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDeleteType(scope.row)"
                v-hasPermi="['dms:dise_catalog:remove']"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-aside>

      <!--右边疾病表-->
      <el-main>
        <el-row style="margin-bottom: 3px">
          <el-col :span="5">
            <el-card shadow="6">
              当前类别：<span v-text="diseName"></span>
            </el-card>
          </el-col>
          <el-col :span="1.5" style="margin-top: 8px">
            <el-button
              type="text"
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['dms:dise:add']"
            >新增诊断</el-button>
          </el-col>
        </el-row>
        <el-table v-loading="loading" :data="diseList">
          <el-table-column label="疾病编码" align="center" prop="code" />
          <el-table-column label="疾病名称" align="center" prop="name" />
          <el-table-column label="ICD编码" align="center" prop="icd" />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['dms:dise:edit']"
              >修改</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['dms:dise:remove']"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-main>
    </el-container>

    <!-- 添加或修改诊断目录管理对话框 -->
    <el-dialog :title="titleType" :visible.sync="openType" width="500px" append-to-body>
      <el-form ref="form" :model="formType" :rules="rulesType" label-width="100px">
        <el-form-item label="疾病分类名" prop="name">
          <el-input v-model="formType.name" placeholder="请输入疾病分类名" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormType">确 定</el-button>
        <el-button @click="cancelType">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 添加或修改诊断目录管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="疾病编码" prop="code">
          <el-input v-model="form.code" placeholder="请输入疾病编码" />
        </el-form-item>
        <el-form-item label="疾病名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入疾病名称" />
        </el-form-item>
        <el-form-item label="ICD编码" prop="icd">
          <el-input v-model="form.icd" placeholder="请输入ICD编码" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDise, getDise, delDise, addDise, updateDise, exportDise } from "@/api/dms/dise";//诊断疾病表
import { listDise_catalog, getDise_catalog, delDise_catalog, addDise_catalog, updateDise_catalog } from "@/api/dms/dise_catalog";//疾病分类表

export default {
  name: "Dise",
  name: "Dise_catalog",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 诊断目录管理表格数据
      diseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        catId: null,
      },
      // 表单参数
      form: {
        catId: null,
      },
      // 表单校验
      rules: {
        code: [
          { required: true, message: "疾病编码不能为空！", trigger: "blur" }
        ],
        name: [
          { required: true, message: "疾病名称不能为空！", trigger: "blur" }
        ],
        icd: [
          { required: true, message: "ICD编码不能为空！", trigger: "blur" }
        ],
      },
      /** 分类 */
      // 分类遮罩层
      loadingType: true,
      // 诊断分类目录管理表格数据
      dise_catalogList: [],
      //  分类查询参数
      queryParamsType: {
        name: null,
      },
      //  分类表单参数
      formType: {},
      //  分类表单效验
      rulesType: {
        name: [
          { required: true, message: "疾病分类名称不能为空！", trigger: "blur" }
        ],
      },
      //  分类弹出层标题
      titleType:"",
      //  分类显示搜索条件
      showSearchType: true,
      // 是否显示弹出层
      openType: false,

      // 当前分类目录名
      diseName:null,

    };
  },
  created() {
    this.getList();
    this.getListType();
  },
  methods: {
    /** 分类目录 */
    getListType() {
      this.loadingType = true;
      if(this.queryParamsType.name == "" || this.queryParamsType.name == null){
        this.diseName = "";
        this.queryParams.catId = null;
        this.getList();
      }
      listDise_catalog(this.queryParamsType).then(response => {
        this.dise_catalogList = response.rows;
        this.loadingType = false;
      });
    },
    /** 搜索按钮操作 */
    handleQueryType() {
      this.getListType();

    },
    /** 新增按钮操作 */
    handleAddType() {
      this.resetType();
      this.openType = true;
      this.titleType = "添加诊断目录管理";
    },
    /** 修改按钮操作 */
    handleUpdateType(row) {
      this.resetType();
      const id = row.id || this.ids
      getDise_catalog(id).then(response => {
        this.formType = response.data;
        this.openType = true;
        this.titleType = "修改分类目录";
      });
    },
    /** 新增目录的提交按钮 */
    submitFormType() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.formType.id != null) {
            updateDise_catalog(this.formType).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.openType = false;
                this.getListType();
              }
            });
          } else {
            addDise_catalog(this.formType).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.openType = false;
                this.getListType();
              }
            });
          }
        }
      });
    },
    /** 左边删除按钮操作 */
    handleDeleteType(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除诊断目录管理编号为"' + row.name + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return delDise_catalog(ids);
      }).then(() => {
        this.queryParamsType.name = "";//清空类别名
        this.getListType();//刷新左边目录
        this.reset();
        this.diseName = "";//清空当前类别名
        this.queryParams.catId = null;
        this.getList();//刷新右边诊断疾病
        this.msgSuccess("删除成功");
      }).catch(function() {});
    },
    // 取消按钮
    cancelType() {
      this.openType = false;
      this.resetType();
    },
  // 表单重置
    resetType() {
      this.formType = {
        id: null,
        name: null,
        status: 0
      };
      this.resetForm("form");
    },

    /** *****************************************/
    /** 查询诊断目录管理列表 */
    getList() {
      this.loading = true;
      listDise(this.queryParams).then(response => {
        this.diseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        catId: null,
        code: null,
        name: null,
        icd: null,
        status: 0
      };
      this.resetForm("form");
    },
    /** 分类目录 点击 操作 */
    handleQuery(data) {
      this.diseName = data.name;
      this.queryParams.catId = data.id;
      this.getList();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加诊断目录管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDise(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改诊断目录管理";
      });
    },
    /** 新增诊断的提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null && this.form.catId != null) {
            updateDise(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else if (this.form.catId != null) {
            addDise(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }else {
            this.msgError("请选择一个诊断分类！！！");
            this.open = false;
            this.getList();
          }
        }
      });
    },
    /** 右边删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除诊断目录管理编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delDise(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
  }
};
</script>
